/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package ServiceOfSOAP;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import javax.jws.WebService;

//Service Implementation
@WebService(endpointInterface = "ServiceOfSOAP.ShoppingCartSoap")
public class ShoppingCartSoapImpl implements ShoppingCartSoap {

    public static ArrayList<Product> products = new ArrayList();
    public static HashMap<Integer, HashMap<String, Integer>> clients = new HashMap<Integer, HashMap<String, Integer>>();

    public ShoppingCartSoapImpl() {
        Product pr1 = new Product("tao1", 12, 3);
        Product pr2 = new Product("xoai1", 7, 6);
        Product pr3 = new Product("oi1", 3, 5);
        Product pr4 = new Product("man", 23, 3);
        Product pr5 = new Product("cam", 7, 90);
        Product pr6 = new Product("na", 35, 5);
        products.add(pr1);
        products.add(pr2);
        products.add(pr3);
        products.add(pr4);
        products.add(pr5);
        products.add(pr6);
    }
    HashMap<String, Integer> cart = null;

    public ArrayList<Product> getProducts() {
        return this.products;
    }

    @Override
    public String getAllProductsText() {
        StringBuffer str = new StringBuffer();
        for (Object obj : this.products.toArray()) {
            Product pro = (Product) obj;
            str.append(pro.getName() + "-");
            str.append(pro.getPrice() + "-");
            str.append(pro.getAmount());
            str.append(":");
        }
        return str.substring(0, str.length() - 1).toString();
    }

    @Override
    public String addUser(int UserID) {
        String check = "";
        cart = new HashMap<String, Integer>();
        if (cart != null) {
            System.out.println("Cart  duuoc tao ra ");
        }
        this.clients.put(new Integer(UserID), cart);
        System.out.println("tao ra " + this.clients.size());
        return new StringBuilder().append(UserID).append(check).toString();
    }

    @Override
    public String addProduct(int UserID, String ProductID) {
        System.out.println("chieu dai cua list" + this.products.size());
        cart = this.clients.get(UserID);
        //addItem
        if (cart.containsKey(ProductID)) {
            int newQuantity = ((Integer) cart.get(ProductID)).intValue() + 1;
            cart.remove(ProductID);
            cart.put(ProductID, newQuantity); //update cart
        } else {
            Integer in = new Integer(1);
            cart.put(ProductID, in);
        }
        System.out.println("Chieu dai cua cart" + cart.size());
        this.clients.remove(UserID);
        this.clients.put(UserID, cart);//undapte list of user
        StringBuilder str = new StringBuilder();
        str.append(UserID);
        str.append(ProductID);
        return str.toString();
    }

    @Override
    public void removeProduct(int UserID, String ProductID) {
        cart = this.clients.get(UserID);
        int newQuantity = ((Integer) (cart.get(ProductID))).intValue() - 1;
        cart.remove(ProductID);
        cart.put(ProductID, newQuantity);//update cart
        this.clients.remove(UserID);
        this.clients.put(UserID, cart);//undapte list of user
        // return this.getAllProducts();
    }

    @Override
    public String checkOutText(int UserID) {
        cart = this.clients.get(UserID);
        Iterator productIDs = cart.keySet().iterator();
        while (productIDs.hasNext()) {
            String isAvailable = "false";
            String productId = (String) productIDs.next();
            int quantity = (Integer) cart.get(productId).intValue();
            for (Object obj : this.products.toArray()) {
                Product product = (Product) obj;
                if (product.getName().equals(productId)) {
                    int newAmount = product.getAmount() - quantity;
                    if (newAmount == 0) {
                        this.products.remove(product);
                        isAvailable = "true";
                    } else if (newAmount > 0) {
                        product.setAmount(newAmount);
                        isAvailable = "true";
                    } else {
                        break;
                    }

                }
            }
            if (isAvailable == "false") {
                this.clients.put(UserID, new HashMap<String, Integer>());
                return "false";
            }
        }
        this.clients.put(UserID, new HashMap<String, Integer>());
        return "true";
    }

}
